Grokking Scalable Systems for Interviews
Rate this course
Ask Author
Contribute
Back to course home

4% completed

Vote For New Content
Introduction
Course Overview
Who This Course Is for
Caching
What Is Negative Caching and When Should You Cache 404 or Empty Results?
What Is the Difference Between Soft TTL and Hard TTL, and Why Does It Matter?
How Can a Bloom Filter Reduce Cache or Database Load?
What Is Distributed Locking for Cache Rebuilds, and How Does It Prevent Cache Stampedes?
Load Balancing
What Are Sticky Sessions (Session Affinity) and When Should I Avoid Them?
What Is the Difference between Liveness Checks and Readiness Checks in Load Balancers?
What is Global Server Load Balancing (GSLB) and How GeoDNS and Anycast Work?
API Basics
What are the Main API Pagination Strategies (offset, cursor, keyset), and When Should I Use Each?
What are HTTP Conditional Requests (ETag, If‑None‑Match, Last‑Modified) and How They Reduce Load?
What Is the Difference between Rate Limiting and Throttling and Quotas?
What Are Idempotency Keys and How to Implement Them Safely for Payments
Databases
What are SQL Isolation Levels (Read Committed, Repeatable Read, Serializable), and What Anomalies Do They Prevent?
What Is a Write‑Ahead Log (WAL), and How Does It Ensure Durability?
What Is the Difference Between Synchronous and Asynchronous Replication, and When Should I Use Read Replicas?
What Is MVCC (Multi‑Version Concurrency Control), and How Does It Enable Concurrent Reads and Writes?
What Are Safe Patterns for Online Schema Changes vs. Offline Migrations?
Scalability
What is Little’s Law and How to Use It for Quick Capacity Estimates in System Design
How Can I do Back-of-the-Envelope Sizing for QPS, Bandwidth, Storage, and Peak vs. Average Load?
What are Cold Starts and Warm Starts, and Why Do They Matter for Performance?
Sharding and Partitioning
How Do I Choose a Good Shard Key and Avoid Hotspots (Bucketing and Key Randomization)?
What Are Range Sharding, Directory‑Based Sharding, and Geo‑Sharding, and What Are the Common Use Cases?
What Is the Difference Between Rendezvous Hashing and Consistent Hashing, and When Should I Use Each?
Messaging and Streaming
What Do At‑most‑once, At‑least‑once, And Exactly‑once Delivery Semantics Mean?
What Are Idempotent Producers And Consumers, And How Do De‑duplication Keys Work?
What Is Message Ordering, How Do Partition Keys Affect It, And When Can Ordering Break?
What Are Windowing And Watermarking In Streaming Systems, In Simple Terms?
Consistency and Replication
What Is Quorum (N, R, W), And Why Does R + W > N Give Strongly Consistent Reads?
What Are Common Conflict Resolution Strategies (Last‑write‑wins, Vector Clocks, Logical Clocks)?
What Are Read Repair, Hinted Handoff, And Anti‑entropy (Merkle Trees) In Eventually Consistent Systems?
Observability
What Is The Difference Between SLI, SLO, And SLA, And Can You Give Simple Examples?
What Is Opentelemetry, And How Do Traces, Spans, Metrics, And Logs Fit Together?
What Is An Error Budget, And How Should It Guide Release Decisions?
Security
What Are The Trade‑offs Between Server‑stored Sessions And JWTs for Authentication?
What Is The Difference Between TLS And MTLS, And When Is MTLS Appropriate For Service‑to‑service Auth?
What Is Secrets Management, And How Do Environment Variables, KMS, And Vault Compare?
What Is A Replay Attack, And How Is It Different from Idempotency Issues?
What Are The Differences Between TCP, UDP, And QUIC, And When Should I Use Each?
What Is The Difference Between A Forward Proxy, A Reverse Proxy, And NAT?
What Changed From HTTP/1.1 To HTTP/2 To HTTP/3, And What Are Head‑of‑line Blocking And Multiplexing?
What Is the Difference Between Content‑Addressable Storage and Location‑Based Addressing?
What Are Checksums and Etags, and How Do MD5/SHA Compare to CRC for Integrity?
What Are Hot, Warm, Cold, and Archive Storage Tiers, and When Should I Use Each?
What Is the Outbox Pattern, and How Does Change Data Capture (CDC) Work at a High Level?
What Is CQRS (Command Query Responsibility Segregation), and When Should I Consider It?
What Are the Differences Between a Circuit Breaker, Retry With Backoff, and Rate Limiting?
What Are RPO and RTO, and How Do They Differ in Disaster Recovery Planning?
What Is the Difference Between Active‑Active and Active‑Passive Architectures?
What Is Graceful Degradation, and How Do Feature Flags Help Availability?
What Are the Differences Between UUID, ULID, KSUID, and Snowflake IDs, and How Do I Choose?
How Do Snowflake‑Style IDs Work (Timestamp, Worker, Sequence), and What Problems Do They Solve?
What Is the Difference Between Wall‑Clock Time and Monotonic Time, and Why Is Clock Skew Hard?
Final Words
Key Takeaways
What To Learn Next
What Are The Trade‑offs Between Server‑stored Sessions And JWTs for Authentication?

Server-stored sessions vs JWT authentication refers to a stateful vs stateless approach to keeping users logged in: server-side sessions store user data on the server (making revocation and security control easier but requiring server memory and coordination), whereas JWTs (JSON Web Tokens) store data in a signed token on the client (allowing stateless, scalable authentication at the cost of more complex token management and revocation challenges).

Session-Based Authentication (Server-Stored Sessions)

Session-based authentication (also called cookie-based or stateful authentication) keeps track of user login state on the server.

When a user logs in with valid credentials, the server creates a session record (typically in memory or a database) containing the user’s ID and other info (login time, expiry, etc.).

The server then sends a session ID (usually a long random string) back to the client as a browser cookie.

On subsequent requests, the browser automatically includes this session ID cookie, and the server looks it up in the session store to retrieve the user’s details.

In short, the server holds the authoritative session data, and the cookie is just a key to that data.

Advantages (Pros) of Server Sessions

This approach is simple and reliable. The server has a centralized source of truth for authentication state. It’s easy to revoke sessions in real-time if needed: for example, logging a user out or invalidating a compromised session is as simple as deleting the session record on the server, after which any further requests with that session ID will be rejected.

Session IDs are usually random opaque strings, so they don’t reveal user data and are small in size (minimal bandwidth overhead).

Also, frameworks often support sessions out-of-the-box, making them straightforward to implement.

Disadvantages (Cons) of Server Sessions

Because the server must check a session store (database or memory) on every request, this can introduce latency and scaling issues for large applications.

In a distributed or microservices environment, sessions require either “sticky” load balancing (sending a user to the same server that holds their session) or a shared session database, which adds complexity and overhead.

Each active session consumes server memory or storage, so high resource usage can be a concern as user count grows.

There are also security considerations: if an attacker steals a user’s session cookie (e.g. via XSS or sniffing an unencrypted connection), they can hijack the session.

Moreover, cookies used for sessions are vulnerable to CSRF (Cross-Site Request Forgery) attacks, so developers must implement protections (like same-site cookies or CSRF tokens).

Here’s a clear comparison table between Server-Stored Sessions and Token-Based Authentication (JWT):

AspectServer-Stored SessionsToken-Based Authentication (JWT)
Where session data livesStored on the server (in memory, DB, or cache).Stored on the client (inside the JWT token).
How it’s identifiedA session ID is stored in a cookie; server looks it up.A JWT is sent with each request (usually in header or cookie).
ScalabilityHarder to scale; session data must be shared across servers (e.g., via Redis).Easier to scale; stateless, no shared session store needed.
StatefulnessStateful: server keeps track of each user session.Stateless: server doesn’t store session info; it validates the token only.
Storage requirementsServer memory or DB usage grows with active users.No per-user storage on the server; only signing key is stored.
PerformanceSlightly slower at scale due to session lookup.Faster since token verification doesn’t require a DB read.
SecurityEasier to revoke (delete session on server).Harder to revoke until token expires (unless using blacklist).
Expiration / RenewalControlled by server; can expire or extend anytime.Controlled by token’s exp claim; needs refresh tokens for renewal.
Best suited forTraditional web apps with few servers.APIs, microservices, mobile apps, distributed systems.
Implementation simplicitySimple to implement with frameworks like Express, Django, etc.Requires signing, verifying, and handling token rotation securely.

Token-Based Authentication with JWT (Stateless Tokens)

Token-based authentication uses JWTs (JSON Web Tokens) to maintain login state on the client side.

After a user logs in successfully, the server issues a signed token (the JWT) that encodes the user’s identity and any relevant claims (like roles or expiration time) in a JSON payload.

This token is then stored on the client (for example, in localStorage or a cookie) and sent with each request (often in an HTTP Authorization: Bearer <token> header).

The server does not store session info; instead, it verifies the token’s signature and data on each request to authenticate the user.

JWTs are self-contained. All the information needed to verify the user is inside the token itself, so no database lookup is required during request processing.

Advantages of JWTs:

Because JWTs eliminate per-request database lookups, they can reduce latency and improve performance for APIs. The server just validates the token locally, which is typically fast (a quick cryptographic signature check).

This makes JWTs highly scalable and suitable for distributed systems or microservices, since any server instance can validate the token without sharing session state. JWTs also carry useful info in their payload (e.g. user role, permissions), so the server can make authorization decisions without extra database queries.

They work well for mobile and single-page applications and cross-domain or single sign-on (SSO) scenarios, where storing state on a central server is inconvenient. The same token can be used across multiple domains or services as long as those services trust the issuing server’s signature.

Additionally, JWTs are signed (or encrypted) to prevent tampering, and can be used over HTTPS to maintain confidentiality, making them secure against data modification (if implemented correctly).

Disadvantages of JWTs

The biggest drawback is that JWTs are stateless tokens that cannot be easily revoked or changed on the fly.

Once a JWT is issued, it remains valid until it expires. The server generally cannot pull it back or update the claims inside it. This means revoking a user’s access (e.g. logging out from all devices or wngrading a user’s role) is hard to do immediately; you usually have to wait until the JWT’s expiration time or implement a custom blacklist which undermines the stateless design.

For better security, JWT-based systems often use short-lived tokens (e.g. 15 minutes) with refresh tokens to balance user convenience with the ability to expire credentials.

Another concern is that JWTs, being self-contained, often have a larger size than a simple session ID cookie sending this extra data on every request adds network overhead (usually small, but noteworthy).

Also, if a JWT is stored in the browser (e.g. localStorage), it can be vulnerable to XSS attacks (JavaScript can potentially steal it), whereas if stored in a cookie it could be vulnerable to CSRF (though you can use HttpOnly/SameSite cookies to mitigate some risks).

In short, JWTs put more responsibility on developers to handle security (token storage, rotation, etc.), and bugs or misconfigurations (like using weak signing secrets or not checking token signatures correctly) can introduce vulnerabilities.

Sessions vs JWT: Key Trade-offs and Differences

Both methods solve the problem of maintaining user authentication in a stateless HTTP world, but they do so in opposite ways.

Here are the key trade-offs between server-stored sessions and JWTs:

  • Stateful vs Stateless: Server sessions are stateful. The server holds session data and thus “remembers” each user between requests. JWTs are stateless. All state is in the token on the client, so the server keeps no per-user memory. This means sessions require managing state (which can complicate scaling), while JWTs let you scale out easily since any server can accept a valid token without shared storage.

  • Scalability and Architecture: Because of the above, sessions can become challenging in distributed systems or cloud environments. If you have multiple servers, you need a centralized session store or sticky sessions, which adds complexity and potential performance hits. JWTs shine here. They are ideal for microservices and APIs where you might not want a single point of truth for sessions. Each service can verify the token independently, making JWTs highly scalable for modern applications. This is one reason JWTs are popular for RESTful APIs and mobile apps.

  • Security and Control: With server-side sessions, the server has tight control. You can enforce security checks on every request (since you consult the session store each time) and you can invalidate sessions at will (delete the session record to log the user out immediately). JWTs trade some of that control for decentralization: since the server only checks the token signature, it generally trusts the token until expiration, making immediate revocation difficult. In practice, applications using JWTs must design around this (short token lifespans, revocation lists, etc.). Also, consider that a session ID is opaque and meaningless to clients, while a JWT’s payload can be decoded by anyone. It’s usually not encrypted, only encoded and signed. So sensitive data should never be put in a JWT’s payload, and any user info in it (like roles or usernames) is exposed to the client (though still protected from tampering by the signature). Sessions do not have this info visible on the client side (it stays on the server).

  • Performance and Overhead: Using sessions means an extra database or cache lookup on each request to retrieve the session, which adds a bit of latency and server load. JWT verification is typically faster for each request (no DB call, just a cryptographic verify) and can reduce overall latency in high-throughput systems. However, JWTs increase network overhead slightly. The token (which might be a few hundred bytes or more) gets sent with every request, whereas a session ID is very small. If the JWT contains a lot of claims or is poorly designed, it could bloat your request headers. In most cases this size difference is minor, but it’s a consideration for extremely performance-sensitive or low-bandwidth scenarios.

  • Implementation Complexity: Session-based auth is mature and widely supported by web frameworks, setting cookies and storing sessions is straightforward. The trade-off is you must run a session store and ensure its availability. JWTs require careful implementation of token issuing and verification. Developers need to manage secrets/keys, handle token expiration and refreshing, and safely store tokens on the client. This offers more flexibility (you can include custom data in JWTs and have clients or other services use them), but there’s more that can go wrong. Debugging JWT issues or rotating signing keys can be tricky compared to simple session cookies. Thus, JWTs might introduce more development overhead and learning curve, especially for beginners, in exchange for the benefits of stateless auth.

Practical Scenarios and Use Cases

Choosing between sessions and JWTs often depends on your app’s needs:

  • Traditional Web Apps (Monolithic): If you have a server-rendered website (same-origin front end and back end), for example, a classic e-commerce site or forum, server sessions are usually ideal. They are easy to use, and you likely won’t have scaling issues until you have a very large user base. You also get quick session invalidation (important for things like logging out users instantly on password change or suspicious activity).

  • Microservices and APIs: For a REST API or a microservice architecture where many services need to authenticate the user, JWTs are convenient. A user can log in to an auth service and receive a token, then every service in the network can trust that token without a central session database. This decoupling is great for scalability and for cross-domain scenarios (like an ecosystem of services on different subdomains or domains). For example, an API serving a mobile app often uses JWTs so that the mobile client just stores a token and sends it with each request.

  • Single Page Applications (SPA): Modern SPAs (Angular, React, etc.) that talk to a separate backend via APIs frequently use JWTs. Since the front-end and back-end might be separate domains or at least separate deployment units, managing cookies can be more complex. JWTs let the SPA treat the backend as a stateless API. However, if the SPA and backend are same-origin, you could still use sessions via cookies. It depends on your preference and security needs.

  • Security-Sensitive Apps: Applications like banking systems or admin dashboards, where you need tight control over user sessions and immediate revocation of access, often lean toward server-stored sessions. The ability to promptly kill a session (and require a fresh login) if something seems off is a big plus for security. These systems might accept the extra overhead of stateful sessions in exchange for maximal control.

  • Hybrid Approaches: It’s not always either-or. Some systems use both: for instance, a server may issue a short-lived JWT for stateless auth but still keep a server session reference to allow force logout. Another approach is using a session cookie along with a JWT for different levels of trust (as described by some authentication providers). The bottom line is you can combine techniques to get the best of both, for example, using JWTs for less sensitive interactions to reduce server calls, but falling back to a server check for critical actions.

What Is An Error Budget, And How Should It Guide Release Decisions?
What Is The Difference Between TLS And MTLS, And When Is MTLS Appropriate For Service‑to‑service Auth?
Mark as Completed

On this page

Session-Based Authentication (Server-Stored Sessions)

Advantages (Pros) of Server Sessions

Disadvantages (Cons) of Server Sessions

Token-Based Authentication with JWT (Stateless Tokens)

Advantages of JWTs:

Disadvantages of JWTs

Sessions vs JWT: Key Trade-offs and Differences

Practical Scenarios and Use Cases